package com.cynteka.cyfoman.core.collection;

import com.sun.javaws.security.Resource;
import org.apache.commons.scxml.model.SCXML;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * User: Sergey
 * Date: 29.07.11
 * Time: 11:30
 */
public class CollectionRegistry {
    private Map<String, ICollection<?>> collections = new HashMap<String, ICollection<?>>();
    private Map<String, ILink<?, ?>> links = new HashMap<String, ILink<?, ?>>();
    private Map<String, SCXML> stateMachines;

    public CollectionRegistry(Collection<ICollection> collections, Collection<ILink> links) {
        for (ICollection collection : collections) {
            addCollection(collection);
        }
        for (ILink link : links) {
            addLink(link);
        }
    }

    public CollectionRegistry(Collection<IRegistryAppender> iRegistryAppenders) {
        for (IRegistryAppender appender : iRegistryAppenders) {
            for (ICollection<?> collection : appender.getCollections()) {
                addCollection(collection);
            }
            for (ILink<?, ?> link : appender.getLinks()) {
                addLink(link);
            }
        }
    }

    protected void addCollection(ICollection<?> collection) {
        if (collection instanceof RegistryItem) {
            RegistryItem registryItem = (RegistryItem) collection;
            collections.put(collection.getId(), collection);
            registryItem.setOwner(this);
        } else {
            throw new IllegalArgumentException(String.format("Incompatible class of collection %1s", collection.getClass().getName()));
        }
    }

    protected void addLink(ILink<?, ?> link) {
        if (link instanceof RegistryItem) {
            RegistryItem registryItem = (RegistryItem) link;
            links.put(link.getId(), link);
            registryItem.setOwner(this);
        } else {
            throw new IllegalArgumentException(String.format("Incompatible class of link %1s", link.getClass().getName()));
        }
    }

    public Collection<ICollection<?>> getCollections() {
        return new ArrayList<ICollection<?>>(collections.values());
    }

    public Collection<ILink<?, ?>> getLinks() {
        return new ArrayList<ILink<?, ?>>(links.values());
    }

    public ICollection<?> getCollection(String id) {
        ICollection res = collections.get(id);
        if(res == null) {
            throw new IllegalArgumentException("Cannot find collection " + id);
        }
        return res;
    }

    public ILink<?, ?> getLink(String id) {
        ILink<?, ?> res = links.get(id);
        if(res == null) {
            throw new IllegalArgumentException("Cannot find link " + id);
        }
        return res;
    }

    public Map<String, SCXML> getStateMachines() {
        return stateMachines;
    }

    public SCXML getStateMachine(String id) {
        return stateMachines.get(id);
    }

    public void setStateMachines(Map<String, SCXML> stateMachines) {
        this.stateMachines = stateMachines;
    }
}
